package main

import (
	"image"
	"image/draw"

	"code.google.com/p/jnj/graphics/affine"
)

type Sprite struct {
	width, height int
	pos           image.Point
	imgs          []image.Image
	flippedimgs   []image.Image
	flipped       bool
	state         int
}

func NewSprite(imgs []image.Image) *Sprite {
	s := &Sprite{}
	s.width = imgs[0].Bounds().Dx()
	s.height = imgs[0].Bounds().Dy()
	s.imgs = imgs

	flip := &affine.Affine{
		{-1, 0, float32(s.width)},
		{0, 1, 0},
	}
	flipped := make([]image.Image, len(imgs))
	for i := range flipped {
		img := image.NewRGBA(imgs[i].Bounds())
		flip.Transform(img, imgs[i], img.Bounds(), nil, draw.Src)
		flipped[i] = img
	}
	s.flippedimgs = flipped

	return s
}

func (s *Sprite) Img() image.Image {
	if s.flipped {
		return s.flippedimgs[s.state]
	}
	return s.imgs[s.state]
}
